<?php

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');
Yaf_Loader::import(EXTEND_PATH.'/qvideo/Videos.php');

class CourseController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->course = new CourseModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function groupAction()
    {
        $gid=$this->getRequest()->get("gid");
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $lists = $this->group->grouplistsAction($user,['page'=>$page,'gid'=>$gid]);
        $this->getView()->assign("lists", $lists);
        $this->getView()->assign("title", '分组');
    }

    public function priorlistsAction()
    {
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $prior = $this->course->priorListsAction($user,$page);
        $this->getView()->assign("prior", $prior);
        $this->getView()->assign("title", '预告');
    }

    public function livelistsAction()
    {
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $live = $this->course->liveListsAction($user,$page);
        $this->getView()->assign("live", $live);
        $this->getView()->assign("title", '直播');
    }

    public function liveAction()
    {
        $llid=$this->getRequest()->get("llid");
        $play = $this->course->playAction($this->authentication['data'],$llid);
        if($play){
            $videos = new Videos();
            $video['url'] = $videos->getPlayUrl($play['channel']);
            $video['sdkAppID'] = $this->sdkAppID;
            $video['accountType'] = $this->accountType;
            $video['nickname'] = $this->authentication['data']['nickname'];
            $video['udid'] = $this->authentication['data']['id'];
            $video['avatar'] = $this->authentication['data']['avatar'];
            $video['tim'] = $this->authentication['tim'];
            $video['channel'] = $play['channel'];
            $this->course->addWatchAction($llid,$this->authentication['data'],2);
            $this->getView()->assign("video", $video);
        }else{
            $this->redirect('/Course/livelists');
        }
        $gift = $this->course->giftListsAction();
        $this->getView()->assign("play", $play);
        $this->getView()->assign("gift", $gift);
        $this->getView()->assign("title", '直播');
    }

    public function backlistsAction()
    {
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $back = $this->course->backListsAction($user,$page);
        $this->getView()->assign("back", $back);
        $this->getView()->assign("title", '回放');
    }

    public function backAction()
    {
        $llid=$this->getRequest()->get("llid");
        $play = $this->course->backAction($this->authentication['data'],$llid);
        if($play){
            $videos = new Videos();
            $video['url'] = $videos->getVideoInfo($play['video']);
            $video['sdkAppID'] = $this->sdkAppID;
            $video['accountType'] = $this->accountType;
            $video['nickname'] = $this->authentication['data']['nickname'];
            $video['udid'] = $this->authentication['data']['id'];
            $video['avatar'] = $this->authentication['data']['avatar'];
            $video['tim'] = $this->authentication['tim'];
            $video['channel'] = $play['channel'];
            $this->course->addWatchAction($llid,$this->authentication['data'],9);
            $this->getView()->assign("video", $video);
        }else{
            $this->redirect('/Course/backlists');
        }
        $this->getView()->assign("play", $play);
        $this->getView()->assign("title", '回放');
    }

    public function enrollAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
            ]);
            if ($validate->check($content)) {
                $user = $this->authentication['data'];
                $data = $this->course->enrollAction($content['llid'],$user);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function scoreAction()
    {
        $content['llid']=$this->getRequest()->get("llid");
        $content['page']=$this->getRequest()->get("page");
        $score = $this->course->scoreAction($content,$this->authentication['data']);
        $play = $this->course->backAction($this->authentication['data'],$content['llid']);
        $this->getView()->assign("title", '评价');
        $this->getView()->assign("score", $score);
        $this->getView()->assign("play", $play);
    }

    public function addScoreAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'score'=>'require|number|between:1,100',
                'comment'=>'require|chsSymbol|max:128',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'score.require' => '分值不能为空',
                'score.number' => '分值类型错误',
                'score.between' => '分值只能在 1 - 100 之间',
                'comment.require' => '评价内容不可为空',
                'comment.chsSymbol' => '评价内容只允许汉字、字母、数字和中文标点符号',
                'comment.max' => '评价内容不能超过128个字符',
            ]);
            if ($validate->check($content)) {
                $data = $this->course->addScoreAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function delScoreAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
            ]);
            if ($validate->check($content)) {
                $data = $this->course->delScoreAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function giftSendAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'cid' => 'require|number|egt:1|exist:config,id',
                'llid' => 'require|number|egt:1|exist:live_list,id',
            ]);
            $validate->message([
                'cid.require' => '礼品不能为空',
                'cid.number' => '礼品类型错误',
                'cid.egt' => '礼品错误',
                'cid.exist' => '礼品不存在',
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
            ]);
            if ($validate->check($content)) {
                $data = $this->course->giftSendAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function giftRecordAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'token' => 'require',
                'page' => 'require|number|egt:1',
            ]);
            $validate->message([
                'token.require' => 'token不能为空',
                'page.require' => '页码不能为空',
                'page.number' => '页码类型错误',
                'page.egt' => '起始页过小',
            ]);
            if ($validate->check($content)) {
                $data = $this->course->giftRecordAction($this->authentication['data'],$content['page']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function passwordAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'password' => 'require|number|length:6|between:100000,999999',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'password.require' => '密码不能为空',
                'password.number' => '密码错误',
                'password.length' => '密码错误',
                'password.between' => '密码错误',
            ]);
            if ($validate->check($content)) {
                $data = $this->course->passwordAction($content,$this->authentication['data']);
                if(!$data){
                    $code = 400;
                    $data['message'] = '密码错误';
                }else{
                    $code = 200;
                    $data['message'] = '验证通过';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }
}
